package 代码记录.指定公司代码练习记录.华为_2024.分值200;

import java.util.Scanner;

/**
 * @author zx
 * @create 2024-02-16 14:41
 * 岛屿的最大面积_695 - 原题(但是自己忘完了)，二刷
 */
public class 可以组成网络的服务器_3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] arr = new int[n][m];
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                arr[i][j] = sc.nextInt();
            }
        }
        int res = maxArea(arr);
        System.out.println(res);
    }
    public static int maxArea(int[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    int s = dfs(grid, i, j);
                    res = Math.max(res, s);//取最大值
                }
            }
        }
        return res;
    }
    //求每个局域网的服务器数量
    private static int dfs(int[][]grid, int i, int j) {
        if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length){
            return 0;
        }
        if(grid[i][j] != 1){
            return 0;
        }
        grid[i][j] = 2;
        return 1 + dfs(grid,i - 1, j) + dfs(grid, i + 1, j)
                + dfs(grid, i, j - 1) + dfs(grid, i, j + 1);

    }
}
